package com.cxy.instarsystem.web;

import com.cxy.instarsystem.service.SerialNoService;
import com.cxy.instarsystem.vo.EncryptResponse;
import com.cxy.instarsystem.vo.SerialNoRequest;
import com.cxy.instarsystem.vo.SerialNoRespose;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@RestController
@RequestMapping("encrypt")
public class EncryptController {
    @Autowired
    private SerialNoService serialNoService;
    @PostMapping("getEncryptSerialNo")
    public EncryptResponse<SerialNoRespose> getEncryptNo(@RequestBody SerialNoRequest request){
        String cpuId = request.getCpuId();
        String serialNo = request.getSerialNo();
        if(serialNoService.checkSerialNo(serialNo, cpuId)) {
            SerialNoRespose serialNoRespose = new SerialNoRespose();
            serialNoRespose.setCpuId(cpuId);
            serialNoRespose.setEncryptSerialNo(serialNo);
            return EncryptResponse.success(serialNoRespose);
        } else {
            return EncryptResponse.error(null, "serial no is not exists");
        }
    }

    @PostMapping("generateSerialNo")
    public List<String> generateSerialNo(@RequestParam("size") Integer size){
        List<String> serialNos = new ArrayList<>();
        for(int i=0; i<size; i++){
            serialNos.add(serialNoService.getSerialNo());
        }
        return serialNos;
    }

    public static void main(String[] args) {
        String[] serialNos = new String[]{
                "meCxjF",
                "xCl3sO",
                "RQer4A",
                "kihGPV",
                "ZTMnrZ",
                "k7H3LN",
                "4iTPgf",
                "18H18Y",
                "EnZ13H",
                "1pQWPk",
                "v0XD4h",
                "EyhGtU",
                "EAvVXt",
                "9mby8K",
                "bOlMN2",
                "Vym8Sy",
                "qbe9Me",
                "XGGpZP",
                "soM9ER",
                "HmUVHw",
                "cj4jay",
                "HdrHEh",
                "iOFOaj",
                "wEV3xw",
                "RJRFHI",
                "t1tI1u",
                "WBCvAC",
                "FMU6qN",
                "jIhDXu",
                "Ta2Hfk",
                "RHlaos",
                "0EhWfV",
                "B85Kll",
                "BwLIOb",
                "arACDd",
                "9EW8Wh",
                "2yKCCg",
                "TVcoV2",
                "DLHgzc",
                "0sURR6",
                "LcqKVz",
                "a9ubpy",
                "fs65ay",
                "KZyVjY",
                "HwQtOu",
                "pHMtNO",
                "dHtlsq",
                "0bcY2n",
                "3fX5d1",
                "KsFaeu",
                "DWphQL",
                "2G9Ok2",
                "st3ZVg",
                "Ipnmpc",
                "eJraL3",
                "ZkTSza",
                "OFLaDX",
                "6eTg03",
                "5MewBk",
                "ZT5P6h",
                "d0Ndxr",
                "HJSFtF",
                "phqtNm",
                "oduiRk",
                "0y8q0V",
                "FN7mWa",
                "5ztOSO",
                "Wkzs5l",
                "nLrTvu",
                "SAfJdy",
                "19zrDE",
                "ufEvQL",
                "sqScW2",
                "Wb5Sxg",
                "hu2zWd",
                "n0Ulmy",
                "PHgEC2",
                "NZC2zI",
                "sli7Zt",
                "jN4sBv",
                "llTKIb",
                "f8dpYt",
                "bE1hRv",
                "ypnAhv",
                "Bv7d1I",
                "CsdLhO",
                "ILRQzo",
                "DoNeVt",
                "ZFUcDk",
                "fbLndz",
                "XE75Qn",
                "Hr5jEN",
                "mfw6if",
                "R9Bm1u",
                "Xb4GDU",
                "hHvbGB",
                "qQLiDr",
                "9MBPT3",
                "8terEm",
                "SILxf0",
                "8h2TJ2",
                "3mIMta",
                "M0IAYJ",
                "CulRUb",
                "6Fgj4Y",
                "K29I6o",
                "pwJ0Rn",
                "0Pfmav",
                "WG5q1R",
                "oCXPQa",
                "l2jWdI",
                "ZcP6qK",
                "zUTE6d",
                "EeXPPD",
                "b4kXXN",
                "FH3hxG",
                "nnNgRC",
                "nfeq63",
                "lhvJF6",
                "1sG7eq"
        };
        String contactName = "袁野";
        String contactTelNo = "13701832209";
        String orginzationName = "福建师范大学数学与信息学院";
        AtomicInteger lastIdEnd = new AtomicInteger();
        String sqlResult = Arrays.stream(serialNos).map(serialNo->{
            String sql = "INSERT INTO t_serial_no VALUES("+(lastIdEnd.incrementAndGet())+", " +
                    "null, " +
                    "'" +contactName+"', "+
                    "'"+contactTelNo+"', "+
                    "'"+LocalDateTime.now()+"', "+
                    "0, "+
                    "1, "+
                    "'"+orginzationName+"', "+
                    "'"+serialNo+"'"+
                    ");";
            return sql;
        }).collect(Collectors.joining("\n"));
        System.out.println(sqlResult);
    }
}
